//
//  YWTopicPicView.m
//  网易新闻
//
//  Created by Yeewii@hotmail.com on 16/6/22.
//  Copyright © 2016年 Yeewii@hotmail.com. All rights reserved.
//

#import "YWTopicPicView.h"
#import "Constants.h"
#import <UIImageView+WebCache.h>
#import <UIView+SDAutoLayout.h>
#import "SDPhotoBrowser.h"



@implementation YWTopicPicView
{
    NSMutableArray *imageViewsArray;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self setUpViews];
    }
    return self;
}


-(void)setUpViews
{

}

-(void)setPicArray:(NSArray *)picArray
{
    _picArray        = picArray;
    if (_picArray.count ==0 )
    {
        self.height = 0;
        self.fixedHeight = @(0);
        return;
    }
    int totalloc     = 3;
    CGFloat appvieww = 100;
    CGFloat appviewh = _picArray.count>1?110: 100;
    CGFloat margin=(MAINWIDTH-totalloc*appvieww)/(totalloc+1);
    BOOL isOne = _picArray.count == 1;
    for (int i=0;i<_picArray.count;i++)
    {
       int row                = i/totalloc;
       int loc                = i%totalloc;
       CGFloat appviewx       = margin+(margin+appvieww)*loc;
       CGFloat appviewy       = margin+(margin+appviewh)*row;
       UIImageView *itemImage = [UIImageView new];
        [self addSubview:itemImage];
        
        UITapGestureRecognizer *picTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
        itemImage.userInteractionEnabled = YES;
        [itemImage addGestureRecognizer:picTap];


        
        
        itemImage.frame = isOne?CGRectMake(0, appviewy, MAINWIDTH, (MAINWIDTH*[_picDic[@"picHeight"] floatValue])/[_picDic[@"picHeight"] floatValue]): CGRectMake(appviewx, appviewy, appvieww, appviewh);
        [itemImage sd_setImageWithURL:[NSURL URLWithString:_picArray[i]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        }];
    }
    if (_picArray.count == 1)
    {
        self.height = (MAINWIDTH*[_picDic[@"picHeight"] floatValue])/[_picDic[@"picHeight"]floatValue]+10;
        self.fixedHeight = @((MAINWIDTH*[_picDic[@"picHeight"] floatValue])/[_picDic[@"picHeight"]floatValue]+10);
    }else if (_picArray.count<4)
    {
        self.height = 130;
        self.fixedHeight = @(130);

    }else if (_picArray.count<7)
    {
        self.height = 280;
        self.fixedHeight = @(280);

    }else
    {
        self.height = 400;
        self.fixedHeight = @(400);

    }
    
}

-(void)setPicDic:(NSDictionary *)picDic
{
    _picDic = picDic;
}


-(void)tap
{
            SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    
            browser.sourceImagesContainerView = self;
    
            browser.imageCount = _picDic.count;
    
            browser.currentImageIndex = 2;
    
            browser.delegate = self;
    
            [browser show]; // 展示图片浏览器
}
@end
